"""
AIPPT PPT Management Service
Handles PPT file management operations
"""
from typing import Dict, Any, Optional, List
from src.llm.aippt_app.utils.api_client import AIPPTAPIClient


class PPTManagementService:
    """Service for managing PPT files"""

    def __init__(self, client: Optional[AIPPTAPIClient] = None):
        """
        Initialize PPT management service

        Args:
            client: AIPPTAPIClient instance, creates new if not provided
        """
        self.client = client or AIPPTAPIClient()

    def list_user_ppts(
        self,
        current: int = 1,
        size: int = 10
    ) -> Dict[str, Any]:
        """
        List user's PPT works with pagination

        Args:
            current: Current page number
            size: Page size

        Returns:
            Paginated PPT list
        """
        try:
            response = self.client.list_pptx(current, size)
            return response
        except Exception as e:
            print(f"Error listing PPTs: {str(e)}")
            raise

    def get_ppt_details(self, ppt_id: str) -> Dict[str, Any]:
        """
        Get complete PPT data

        Args:
            ppt_id: PPT ID

        Returns:
            Complete PPT data
        """
        try:
            response = self.client.load_pptx(ppt_id)
            if response.get('code') == 0 or response.get('data'):
                return response
            else:
                raise Exception(f"Failed to load PPT: {response.get('message')}")
        except Exception as e:
            print(f"Error getting PPT details: {str(e)}")
            raise

    def download_ppt(self, ppt_id: str) -> str:
        """
        Get download URL for PPT file

        Args:
            ppt_id: PPT ID

        Returns:
            Download URL
        """
        try:
            response = self.client.download_pptx(ppt_id)
            if response.get('code') == 0 and response.get('data'):
                download_url = response['data'].get('fileUrl') or response['data'].get('url')
                if download_url:
                    return download_url
                else:
                    raise Exception("Download URL not found in response")
            else:
                raise Exception(f"Failed to get download URL: {response.get('message')}")
        except Exception as e:
            print(f"Error getting download URL: {str(e)}")
            raise

    def delete_ppt(self, ppt_id: str) -> bool:
        """
        Delete a PPT

        Args:
            ppt_id: PPT ID

        Returns:
            True if deletion successful
        """
        try:
            response = self.client.delete_pptx(ppt_id)
            if response.get('code') == 0:
                return True
            else:
                raise Exception(f"Failed to delete PPT: {response.get('message')}")
        except Exception as e:
            print(f"Error deleting PPT: {str(e)}")
            raise
